<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="100px">
      <el-form-item label="优惠券名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入优惠券名称"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="couponList">
      <el-table-column label="用户ID" align="center" prop="id" />
      <el-table-column label="用户账号" align="center" prop="id" />
      <el-table-column label="用户昵称" align="center" prop="id" />
      <el-table-column label="用户等级" align="center" prop="name" />
      <el-table-column label="抽奖次数" align="center" prop="name" />
      <el-table-column label="累计花费猫币" align="center" prop="name" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200px">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleUpdate(scope.row)" >查看中奖明细</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改优惠券管理对话框 -->
    <el-dialog title="查看中奖明细" :visible.sync="open" width="40%" append-to-body>
      <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="100px">
        <el-form-item label="抽奖时间" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入优惠券名称"
            clearable
            size="small"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="loading" :data="couponList">
        <el-table-column label="抽奖时间" align="center" prop="id" />
        <el-table-column label="中奖内容" align="center" prop="id" />
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import { listCoupon, getCoupon, delCoupon, addCoupon, updateCoupon, exportCoupon } from "@/api/system/coupon";
import { loadTeamCategoryItem } from "@/api/system/teamCategoryItem";

export default {
  name: "Coupon",
  data() {
    return {
      teamCategoryOptions:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 优惠券管理表格数据
      couponList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        couponType: undefined,
        couponNumber: undefined,
        couponMoney: undefined,
        sill: undefined,
        sillMoney: undefined,
        expirationType: undefined,
        expirationVal: undefined,
        skillType: undefined,
        remarks: undefined,
        createDate: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "优惠券名称不能为空", trigger: "blur" }
        ],
        couponType: [
          { required: true, message: "可用品类不能为空", trigger: "blur" }
        ],
        couponNumber: [
          { required: true, message: "总发行量不能为空", trigger: "blur" }
        ],
        couponMoney: [
          { required: true, message: "面额不能为空", trigger: "blur" }
        ],
        sill: [
          { required: true, message: "使用门槛不能为空", trigger: "blur" }
        ],
        expirationTimes: [
          { required: true, message: "请选择有效日期", trigger: "blur" }
        ],
        expirationVal: [
          { required: true, message: "固定天数不能为空", trigger: "blur" }
        ],
        skillCategoryIds: [
          { required: true, message: "请选择技能品类", trigger: "blur" }
        ],
        expirationType: [
          { required: true, message: "有效类型不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();

    loadTeamCategoryItem().then(response => {
      this.teamCategoryOptions = response.data;
    });
  },
  methods: {
    getTeamCategoryName(id){
      let name = ''
      this.teamCategoryOptions.forEach(e=>{
        if(e.id === id){
          name = e.name
        }
      })
      return name
    },
    /** 查询优惠券管理列表 */
    getList() {
      this.loading = true;
      listCoupon(this.queryParams).then(response => {
        this.couponList = response.rows;
        this.total = parseInt(response.total);
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        couponType: 1,
        couponNumber: undefined,
        couponMoney: undefined,
        sill: 1,
        sillMoney: undefined,
        expirationType: 1,
        expirationTime: [],
        expirationVal: undefined,
        skillType: 1,
        skillCategoryIds:[],
        remarks: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加优惠券管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCoupon(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改优惠券管理";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateCoupon(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addCoupon(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除优惠券管理编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCoupon(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有优惠券管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportCoupon(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>